package fit.deni.hibernate;

import java.util.Collection;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fit.deni.beans.Reditelj;
import fit.deni.hibernate.interfaces.RediteljService;

public class RediteljServiceImpl implements RediteljService {

	private Session session;

	public RediteljServiceImpl(Session session) {
		this.session = session;
	}

	@CommitAfter
	public void addDirector(Reditelj reditelj) {
		session.merge(reditelj);
	}

	public Collection<Reditelj> getAll() {
		return session.createCriteria(Reditelj.class).add(Restrictions.eq("deleted", false)).list();
	}

	@CommitAfter
	public void deleteDirector(Reditelj reditelj) {
		reditelj.setDeleted(true);
		session.merge(reditelj);
//		session.delete(reditelj);
//		session.flush();
	}

	public Reditelj getDirectorById(int id) {
		return (Reditelj) session.createCriteria(Reditelj.class).add(Restrictions.eq("id", id)).list().get(0);
	}

	public Reditelj getDirectorByName(String name) {
		return (Reditelj) session.createCriteria(Reditelj.class).add(Restrictions.eq("ime", name)).list().get(0);
	}

}
